package com.huang.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huang.component.RedisComponent;
import com.huang.entity.config.AppConfig;
import com.huang.entity.contant.Constants;
import com.huang.entity.dto.SysSettingsDto;
import com.huang.entity.po.EmailCode;
import com.huang.entity.po.UserInfo;
import com.huang.exception.BusinessException;
import com.huang.mapper.EmailCodeMapper;
import com.huang.mapper.UserInfoMapper;
import com.huang.utils.StringTools;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class EmailService extends ServiceImpl<EmailCodeMapper, EmailCode> {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private RedisComponent redisComponent;

    public void sendEmailCode(String toEmail, Integer type) {
        //如果是注册，校验邮箱是否已存在
        if (Objects.equals(type, Constants.ZERO)) {
            UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<>(UserInfo.class).eq(UserInfo::getEmail, toEmail));
            if (null != userInfo) {
                throw new BusinessException("邮箱已经存在");
            }
        }

        String code = StringTools.getRandomNumber(Constants.LENGTH_5);
        sendEmailCode(toEmail, code);

        baseMapper.update(new LambdaUpdateWrapper<>(EmailCode.class)
                .set(EmailCode::getStatus, Constants.ONE).eq(EmailCode::getEmail, toEmail)
                .eq(EmailCode::getStatus, Constants.ZERO));
        EmailCode emailCode = new EmailCode();
        emailCode.setCode(code);
        emailCode.setEmail(toEmail);
        emailCode.setStatus(Constants.ZERO);
        baseMapper.insert(emailCode);
    }

    private void sendEmailCode(String toEmail, String code) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(appConfig.getSendUserName());
            helper.setTo(toEmail);
            SysSettingsDto sysSettings = redisComponent.getSysSettings();
            helper.setSubject(sysSettings.getRegisterMailTitle());
            helper.setText(String.format(sysSettings.getRegisterMailContent(), code));
            helper.setSentDate(new Date());
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new BusinessException("邮件发送失败");
        }
    }
}
